package com.choudou5.solr.framework.util;

import cn.hutool.crypto.SecureUtil;
import com.choudou5.solr.framework.bean.UserSessionBean;
import com.choudou5.solr.framework.constants.SysConsts;
import com.choudou5.solr.util.IpUtil;
import com.choudou5.solr.util.StrUtil;
import com.choudou5.solr.util.cache.CacheFactory;
import com.choudou5.solr.util.cache.impl.LRUCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * Desc：登录工具类
 * Author：choudou5
 * Date：2018-7-13
 */
public class LoginUtil {

    private static final Logger logger = LoggerFactory.getLogger(LoginUtil.class);

    private static final LRUCache<Long, Integer> LOGIN_FAIL_PHONE_MAP = CacheFactory.newLRUCache(200, 5 * 60 * 1000); //5分钟超时

    public static boolean checkFailLoginCount(Long phone) {
        long count = LOGIN_FAIL_PHONE_MAP.getAccessCount(phone);
        if(count > 10)
            return false;
        return true;
    }

    public static void recordFailLogin(Long phone) {
        if(!LOGIN_FAIL_PHONE_MAP.containsKey(phone)){
            LOGIN_FAIL_PHONE_MAP.put(phone, 1);
        }else{
            LOGIN_FAIL_PHONE_MAP.get(phone);
        }
    }

    public static void removeFailLogin(Long phone) {
        LOGIN_FAIL_PHONE_MAP.remove(phone);
    }


    /**
     * 检查是否已登录
     * @param request
     * @return
     */
    public static boolean checkIsLogin(HttpServletRequest request) {
//        String token = CookieUtil.getCookie(request, SysConsts.HEADER_TOKEN);
//        if(StrUtil.isBlank(token))
//            return false;
        UserSessionBean userSessionBean = UserUtil.getUser();
        if (userSessionBean == null)
            return false;
        String ip = IpUtil.getRealIpAddr(request);
        if(!ip.equals(userSessionBean.getLoginIp()) && validToken(ip, userSessionBean.getToken())){
            logger.debug(ip+" 非法IP操作");
            return false;
        }
        return true;
    }

    public static boolean isStaticRequest(HttpServletRequest request) {
        String url = RequestUtil.getReqUrl(request);
        String[] staticPaths = SysConsts.STATIC_PATH;
        for (String staticPath : staticPaths) {
            if(url.startsWith(staticPath)){
                return true;
            }
        }
        System.out.println("非静态的请求："+url);
        return false;
    }


    public static String genToken(String ip) {
        String uuid = StrUtil.replace(UUID.randomUUID().toString(), "-", "");
        String ipSha1 = getIpSha1(ip);
        return uuid + ipSha1;
    }

    public static boolean validToken(String ip, String token) {
        if(token.length() < 32){
            return false;
        }
        String end = token.substring(32);
        String ipSha1 = getIpSha1(ip);
        if(!ipSha1.equals(end)){
            return false;
        }
        return true;
    }

    private static String getIpSha1(String ip) {
        String ipSha1 = SecureUtil.sha1(ip);
        ipSha1 = ipSha1.substring(0, 5)+ipSha1.substring(10, 15);
        return ipSha1;
    }

    public static void main(String[] args) {
        System.out.println(genToken("12312.234.234"));
        System.out.println(validToken("12312.234.234", "2243acb2af83437a8793bc34c291cd263a151bc910"));
    }

}
